<?php

namespace Apps\AppApi\Method\User;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\Keyword;
use Apps\Common\Libs\MobileSMS;
use Apps\Common\Models\UserBase;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;
use Apps\Common\Libs\SmaUtil;


/**
 * 新用户注册
 * @package Apps\AppApi\Method\User
 */
class Register extends BaseMethod
{

    private $message = '';
    /**
     * @var \Apps\Common\Models\UserBase
     */
    private $userInfo = [];
    /**
     * @var int
     */
    private $success = 0;
    /**
     * @var string
     */
    private $userToken = '';

    public function run()
    {
        $account = $this->getDataItem('account',1);//帐号
        $password = $this->getDataItem('password',1);//密码
        $nickname = Keyword::Filter($this->getDataItem('nickname',1));//昵称
        $smsCode = $this->getDataItem('sms_code','');//短信验证

        if($this->userInfoByAccount($account)){
            $this->setResult([
                'success' => 101,
                'user_token' => $this->userToken,
                'message' => '账户名已存在'
            ]);
            return;
        }

        if(empty($password)||empty($nickname)){
            $this->setResult([
                'success' => 103,
                'user_token' => $this->userToken,
                'message' => '密码和昵称不能为空'
            ]);
            return;
        }
        if (empty($smsCode)) {
            $this->setResult([
                'success' => 102,
                'user_token' => $this->userToken,
                'message' => '短信验证码必填'
            ]);
            return;
        }
        if(!MobileSMS::Check('register', $smsCode, $account, $this->apiAccessToken->id)) {
            $this->setResult([
                'success' => 102,
                'user_token' => $this->userToken,
                'message' => '短信验证码错误'
            ]);
            return;
        }

        $this->userInfo = new UserBase();
        $this->userInfo->user_account = $account;
        $this->userInfo->user_password = $this->hashPassword($password);
        $this->userInfo->user_nickname = $nickname;
        $this->userInfo->user_cover = $this->getConfig()->defaultCover; //'default.jpg';
        $this->userInfo->bind_phone = $account;
        if($this->userInfo->save()){
            $this->success = 1;
            $this->message = '注册成功';

            $this->saveUserAuth($this->apiAccessToken->id, $this->userInfo->user_id);
            $this->userToken = $this->getUserAuth()->userToken;
        }else{
            $this->message = '注册失败';
            $this->databaseErrorLog($this->userInfo);
        }
        if ($this->success == 1) {
            if ($model = UserBase::findFirst('user_id = ' . $this->userInfo->user_id)) {
                $info = $model->toArray();
            } else {
                $info = [];
            }

            $info['user_mobile'] = $info['bind_phone'];
            $info['user_cover'] = $info['user_cover'] ? PicUrl::UserCover($info['user_cover'], $this->getDi()) : '';
            // 获取城市信息
            $SysAreaZipDistrictPhonecode = new SysAreaZipDistrictPhonecode();
            $city = $SysAreaZipDistrictPhonecode->getOne($info['user_district']);
            $info['user_district'] = $city['FULL_NAME'] ?: '';

            //获取用户账号
            $userid = $this->getUserAuth()->userId;
            $mobile = UserBase::findFirst("user_id = {$userid}")->user_account;
            $smautil = new SmaUtil();
            $rs = $smautil->getIdentity($mobile);
            //加入身份
            if ($rs){
                switch ($rs['type']){
                    case 1:
                    case 2:
                        $info['identity'] = 3;
                        break;
                    case 3:
                        $info['identity'] = 1;
                        break;
                    case 4:
                        $info['identity'] = 2;
                        break;
                    default:
                        $info['identity'] = 3;
                        break;
                }
            }
            else {
                $info['identity'] = 3;//普通用户
            }
            // 删除不要的字段
            unset($info['user_account'], $info['user_password'], $info['user_state'], $info['user_addtime'], $info['is_manage']);
            $this->setResult(array_merge($info, [
                'success' => $this->success,
                'user_token' => $this->userToken,
                'message' => $this->message,
            ]));
        } else {
            $this->setResult([
                'success' => $this->success,
                'user_token' => $this->userToken,
                'message' => $this->message,
                'user_id' => $this->userInfo->user_id,
            ]);
        }

    }
}